package com.amway.qy.core.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amway.qy.core.constants.QyWechatUrls;
import com.amway.qy.core.service.QyAccessTokenService;
import com.amway.qy.core.service.QyDepartmentSerivce;
import com.amway.qy.resource.dept.QyDepartment;
import com.amway.qy.resource.result.BaseResult;
import com.amway.qy.resource.result.dept.QyDeptCreateResult;
import com.amway.qy.resource.result.dept.QyDeptListResult;
import com.amway.qy.util.common.QyHttpClientUtils;
import com.google.gson.Gson;

public class QyDepartmentServiceImpl implements QyDepartmentSerivce{
	
	private static final Logger logger = LoggerFactory.getLogger(QyDepartmentServiceImpl.class);
	
	//微信企业号ROOT部门默认ID：1.
	private static final Integer ROOT_DEPT= Integer.valueOf(1);
	
	private QyAccessTokenService accessTokenManager;
	
	// Construct here
	public QyDepartmentServiceImpl(QyAccessTokenService accessTokenManager){
		this.accessTokenManager = accessTokenManager;
	}
	
	@Override
	public QyDeptCreateResult createQyDepartment(QyDepartment dept) {
		return new Gson().fromJson(createQyDepartmentInRaw(dept), QyDeptCreateResult.class);
	}

	@Override
	public String createQyDepartmentInRaw(QyDepartment dept) {
		logger.debug("createQyDepartment -->"+dept);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		return QyHttpClientUtils.httpsPostJson(QyWechatUrls.CREATE_DEPT_URL,paramsMap, new Gson().toJson(dept));
	}

	@Override
	public BaseResult updateQyDepartment(QyDepartment dept) {
		
		return new Gson().fromJson(updateQyDepartmentInRaw(dept), BaseResult.class);
	}

	@Override
	public String updateQyDepartmentInRaw(QyDepartment dept) {
		logger.debug("updateQyDepartmentInRaw -->"+dept);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));	
		return QyHttpClientUtils.httpsPostJson(QyWechatUrls.UPDATE_DEPT_URL,paramsMap, new Gson().toJson(dept));
	}

	@Override
	public BaseResult deleteQyDepartment(Integer deptId) {
		 
		return new Gson().fromJson(deleteQyDepartmentInRaw(deptId), BaseResult.class);
	}

	@Override
	public String deleteQyDepartmentInRaw(Integer deptId) {
		logger.debug("deleteQyDepartmentInRaw: Deparment Id -->"+deptId);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));	
		paramsMap.add(new BasicNameValuePair("id",String.valueOf(deptId)));
		return QyHttpClientUtils.httpsGet(QyWechatUrls.DELETE_DEPT_URL, paramsMap);
	}

	@Override
	public QyDeptListResult listDepartment(Integer deptId) {
		return new Gson().fromJson(listDepartmentInRaw(deptId), QyDeptListResult.class);
	}

	@Override
	public String listDepartmentInRaw(Integer deptId) {
		logger.debug("listDepartmentInRaw: Department Id -->"+deptId);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("id", String.valueOf(deptId)));		
		return QyHttpClientUtils.httpsGet(QyWechatUrls.LIST_DEPT_URL, paramsMap);
	}

	@Override
	public QyDeptListResult listDepartment() {
		 
		return new Gson().fromJson(listDepartmentInRaw(), QyDeptListResult.class);
	}

	@Override
	public String listDepartmentInRaw() {
		
		return listDepartmentInRaw(ROOT_DEPT);
	}

	//----------------------------------Getter and Setter----------------------------------------------------
	public QyAccessTokenService getAccessTokenManager() {
		return accessTokenManager;
	}

	public void setAccessTokenManager(QyAccessTokenService accessTokenManager) {
		this.accessTokenManager = accessTokenManager;
	}

	@Override
	public QyAccessTokenService getQyAccessTokenService() {
		// TODO Auto-generated method stub
		return accessTokenManager;
	}

	@Override
	public void setQyAccessTokenService(QyAccessTokenService tokenService) {
		this.accessTokenManager = tokenService;
	}
	
	
	
}
